package common.dao;


import common.DataExchange;
import common.domain.Student;
import common.domain.StudentMessage;

import java.io.*;
import java.util.*;

import static common.Constance.REG_NAME;
import static common.Constance.REG_PASSWORD;

/**
 * @author LinLe
 * @create 2020-12-17 17:22
 */
//服务器对于管理层对学员信息的操作
public class StuInformationOperator {
    private HashMap<String,Student> stu;
    
    public StuInformationOperator(HashMap<String,Student> stu) {
        this.stu = stu;
    }

    public Map<String,Student> getStu() {
        return stu;
    }
    public void add(DataExchange de) throws IOException, ClassNotFoundException {
        Student sm = de.receiveStudent();
        String name = sm.getName();
        String password = sm.getPassword();
        if(  name.matches(REG_NAME) && password.matches(REG_PASSWORD) && !stu.containsKey(name)){
            stu.put(name,sm);
            de.sendString("添加成功");
        }else{
            de.sendString("该学员已存在");
        }
    }


    public void delete(DataExchange de) throws IOException, ClassNotFoundException {
        String name = de.receiveString();
        if(stu.containsKey(name)){
            stu.remove(name);
            de.sendString("删除成功");
        }else{
            de.sendString("该学员不存在");
        }
    }


    public void find(DataExchange de) throws IOException, ClassNotFoundException {
        String name = de.receiveString();
        if(stu.containsKey(name)){
            de.sendStudentMessage(new StudentMessage(stu.get(name),"SUCCESS"));
        }else{
            de.sendStudentMessage(new StudentMessage(null,"FAIL"));
        }
    }


    public void change(DataExchange de) throws IOException, ClassNotFoundException {
        String name = de.receiveString();
        if (stu.containsKey(name)) {
            de.sendStudent(stu.get(name));
            Student sm = de.receiveStudent();
            String na = sm.getName();
            String password = sm.getPassword();
            if(name.matches(REG_NAME) && password.matches(REG_PASSWORD)) {
                stu.put(na, sm);
                de.sendString("修改成功");
            }else{
                de.sendString("格式不正确");
            }
        }else{
            de.sendString(null);
        }

    }
    
    public void show(DataExchange de) throws IOException {
        if(stu.isEmpty()){
            de.sendString("还没有人");
        }else{
            de.sendString("有人了");
            de.sendCol(stu);

        }
    }
//    public void show(DataExchange de) throws IOException {
//        if(stu.size() == 0){
//            de.sendString("还没有人");
//        }else{
//            de.sendString("有人了");
//            Collection<Student> students = stu.values();
            //传出Student对象
//            students.stream().forEach(student-> {
//                try {
//                    de.sendStudent(student);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            });
//            de.sendStudent(null);
            //改为传字符串可行；
//            StringBuilder stringBuilder = new StringBuilder("");
//            stringBuilder.append(students);
//            de.sendString(stringBuilder.toString());
//            System.out.println(stu);
//
//        }
//    }
   

}
